package com.base.plugin;

import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.codegen.AbstractJavaGenerator;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wangxiaonan
 * @Date: 2018/8/12
 **/
public class RepositoryJavaGenerator extends AbstractJavaGenerator {

    @Override
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<CompilationUnit> getCompilationUnits() {

        String pack = context.getProperty("repositoryTargetPackage");
        String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
        StringBuffer typeSB = new StringBuffer(pack).append(".").append(domainObjectName).append("Dao");
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(typeSB.toString());
        Interface inter = new Interface(type);
        inter.setVisibility(JavaVisibility.PUBLIC);
        inter.addJavaDocLine("/**\n * create by mapper-generator\n */");
        String baseRecordType = introspectedTable.getBaseRecordType();
        inter.addImportedType(new FullyQualifiedJavaType(baseRecordType));
        String baseCriteria = baseRecordType + "Criteria";
        inter.addImportedType(new FullyQualifiedJavaType(baseCriteria));
        inter.addImportedType(new FullyQualifiedJavaType("com.base.support.BaseDao"));


        // 引入继承接口 BaseDao<CdAccfund, CdAccfundCriteria, String>
        StringBuffer superInterface = new StringBuffer("BaseDao<").append(domainObjectName).append(",")
                .append(domainObjectName + "Criteria").append(",").append(" String>");
        inter.addSuperInterface(new FullyQualifiedJavaType(superInterface.toString()));

        List answer = new ArrayList();
        answer.add(inter);
        return answer;
    }

}
